//
//  DongTaiTableView.m
//  1gene
//
//  Created by 1jiyin on 16/3/11.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "DongTaiTableView.h"
#import "UILabel+StringFrame.h"
@implementation DongTaiTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self=[super initWithFrame:frame style:style]) {
        self.backgroundColor=[UIColor grayColor];
        self.delegate=self;
        self.dataSource=self;
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self initData];
    }
    return self;
}

#pragma mark 初始化数据
-(void)initData
{
    NSArray *arr=@[@{@"userHeadImage":@"75x75-1.png",@"userName":@"张三",@"biaoqian":@"吉祥吉送采购负责人",@"content":@"长期收购生鲜、蔬果、禽蛋、欢迎和作伙伴进行洽谈祝大家新年快乐!长期收购生鲜、蔬果、禽蛋、欢迎和作伙伴进行洽谈祝大家新年快乐!长期收购生鲜、蔬果、禽蛋、欢迎和作伙伴进行洽谈祝大家新年快乐!",@"fabiaoPhoto":@[@"myBackgroundImage.png"],@"time":@"1分钟前",@"like":@"12",@"commentNumber":@"100",@"commentArr":@[@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-3.png",@"userName":@"冰冰",@"comment":@"回复李晨新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},]},@{@"userHeadImage":@"75x75-4.png",@"userName":@"张三",@"biaoqian":@"吉祥吉送采购负责人",@"content":@"长期收购生鲜、蔬果、禽蛋、欢迎和作伙伴进行洽谈祝大家新年快乐!",@"fabiaoPhoto":@[@"myBackgroundImage.png",@"myBackgroundImage.png"],@"time":@"1分钟前",@"like":@"12",@"commentNumber":@"100",@"commentArr":@[@{@"headImage":@"75x75-1.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-5.png",@"userName":@"李晨",@"comment":@"新年快乐"}]},@{@"userHeadImage":@"75x75-3.png",@"userName":@"张三",@"biaoqian":@"吉祥吉送采购负责人",@"content":@"长期收购生鲜、蔬果、禽蛋、欢迎和作伙伴进行洽谈祝大家新年快乐!",@"fabiaoPhoto":@[@"myBackgroundImage.png",@"myBackgroundImage.png",@"myBackgroundImage.png",@"myBackgroundImage.png",@"myBackgroundImage.png"],@"time":@"1分钟前",@"like":@"12",@"commentNumber":@"100",@"commentArr":@[@{@"headImage":@"75x75-2.png",@"userName":@"李晨",@"comment":@"新年快乐"},@{@"headImage":@"75x75-6.png",@"userName":@"冰冰",@"comment":@"回复李晨新年快乐"}]}];
    _dataArray=[NSMutableArray arrayWithArray:arr];
}

#pragma mark 代理方法
//每一组的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_dataArray[section][@"commentArr"] count]>4) {
        return 4;
    }else{
        return [_dataArray[section][@"commentArr"] count];
    }
    
}
//组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"dongtaiCell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"dongtaiCell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    //用户头像
    UIImageView *userImageView=[[UIImageView alloc]initWithFrame:CGRectMake(30/2.0, 30/2.0, 30, 30)];
    userImageView.layer.cornerRadius=15.0;
    userImageView.layer.masksToBounds=YES;
    userImageView.image=[UIImage imageNamed:_dataArray[indexPath.section][@"commentArr"][indexPath.row][@"headImage"]];
    [cell.contentView addSubview:userImageView];
    
    //姓名
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userImageView.frame)+25/2.0, 15, self.frame.size.width-CGRectGetMaxX(userImageView.frame)-25/2.0, 30/2.0)];
    name.text=_dataArray[indexPath.section][@"commentArr"][indexPath.row][@"userName"];
    name.textColor=[UIColor blackColor];
    name.font=[UIFont systemFontOfSize:16];
    [cell.contentView addSubview:name];
    
    //标签
    UILabel *biaoqian=[[UILabel alloc]initWithFrame:CGRectMake(name.frame.origin.x, CGRectGetMaxY(name.frame)+10/2.0, name.frame.size.width, 30/2.0)];
    biaoqian.text=_dataArray[indexPath.section][@"commentArr"][indexPath.row][@"comment"];
    biaoqian.font=[UIFont systemFontOfSize:24/2.0];
    biaoqian.textColor=[UIColor grayColor];
    [cell.contentView addSubview:biaoqian];
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self makeView:section];
}
-(UIView *)makeView:(NSInteger)section
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, (130+112+230+70+80)/2.0)];
    view.backgroundColor=[UIColor whiteColor];
    //用户头像
    UIImageView *userImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    userImageView.layer.cornerRadius=25.0;
    userImageView.layer.masksToBounds=YES;
    userImageView.image=[UIImage imageNamed:_dataArray[section][@"userHeadImage"]];
    [view addSubview:userImageView];
    
    //姓名
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userImageView.frame)+25/2.0, 10+10, self.frame.size.width-CGRectGetMaxX(userImageView.frame)-25/2.0, 30/2.0)];
    name.text=_dataArray[section][@"userName"];
    name.textColor=[UIColor blackColor];
    name.font=[UIFont systemFontOfSize:16];
    [view addSubview:name];
    
    //标签
    UILabel *biaoqian=[[UILabel alloc]initWithFrame:CGRectMake(name.frame.origin.x, CGRectGetMaxY(name.frame)+10/2.0, name.frame.size.width, 30/2.0)];
    biaoqian.text=_dataArray[section][@"biaoqian"] ;
    biaoqian.font=[UIFont systemFontOfSize:24/2.0];
    biaoqian.textColor=[UIColor grayColor];
    [view addSubview:biaoqian];
    
    
    //分割线
    UIView *line1=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(userImageView.frame)+10, self.frame.size.width, 0.5)];
    line1.backgroundColor=[UIColor grayColor];
    [view addSubview:line1];
    
    //发表的内容
    UILabel *content=[[UILabel alloc]initWithFrame:CGRectMake(20/2.0, CGRectGetMaxY(line1.frame)+9, self.frame.size.width-20/2.0-20/2.0, 50)];
    content.text=_dataArray[section][@"content"];
    
    content.font=[UIFont systemFontOfSize:32/2.0];
    content.textColor=[UIColor blackColor];
//    content.frame=CGRectMake(20/2.0, CGRectGetMaxY(line1.frame)+9, self.frame.size.width-20/2.0-20/2.0, [self calculateWidthWithLable:content]);
    content.numberOfLines=0;
    content.lineBreakMode= NSLineBreakByWordWrapping;
    
    CGSize size=[content boundingRectWithSize:CGSizeMake(self.frame.size.width-20/2.0-20/2.0, 0)];
    content.frame=CGRectMake(20/2.0, CGRectGetMaxY(line1.frame)+9, self.frame.size.width-20/2.0-20/2.0, size.height);
    [view addSubview:content];
    
    
    
    
    
    
    CGFloat height=CGRectGetMaxY(content.frame);
    //发表的图片
    NSInteger imageCount=[_dataArray[section][@"fabiaoPhoto"] count];
    if (imageCount==1) {
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(content.frame)+10, self.frame.size.width*(230/375.0), self.frame.size.width*(230/375.0))];
        imgView.image=[UIImage imageNamed:_dataArray[section][@"fabiaoPhoto"][0]];
        [view addSubview:imgView];
        height=CGRectGetMaxY(imgView.frame);
    }else if (imageCount>1){
        CGFloat img_w=self.frame.size.width*(230/375.0)/2.0;
        CGFloat img_h=self.frame.size.width*(230/375.0)/2.0;
        //行
        NSInteger hang=[self getHang:imageCount];
        height+=hang*(img_h+5);
        for (int i=0; i<hang; i++) {
            
            for (int j=0; j<3; j++) {
                if (i*3+j<imageCount) {
                    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10+(img_w+5)*j, CGRectGetMaxY(content.frame)+10+(img_h+5)*i, img_w, img_h)];
                    imgView.image=[UIImage imageNamed:_dataArray[section][@"fabiaoPhoto"][i*3+j]];
                    [view addSubview:imgView];
                    [view addSubview:imgView];
                }
                
            }
            
            
        }
    }
    //时间
    UILabel *time=[[UILabel alloc]initWithFrame:CGRectMake(10, height+10, 50, 70.0/2.0)];
    time.text=_dataArray[section][@"time"];
    time.textColor=[UIColor grayColor];
    time.font=[UIFont systemFontOfSize:24/2.0];
    view.frame=CGRectMake(0, 0, self.frame.size.width, CGRectGetMaxY(content.frame)+10);
    [view addSubview:time];
    
    
    //喜欢
    UIImageView *likeImgView=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width-(10+40+20+40+20), time.frame.origin.y+(35-15)/2.0, 20, 15)];
    likeImgView.image=[UIImage imageNamed:@"30X30-3.png"];
    [view addSubview:likeImgView];
    
    UILabel *like=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(likeImgView.frame), time.frame.origin.y, 35, 35)];
    like.text=_dataArray[section][@"like"];
    //like.backgroundColor=[UIColor yellowColor];
    like.textAlignment=NSTextAlignmentCenter;
    like.textColor=[UIColor grayColor];
    like.font=[UIFont systemFontOfSize:12];
    [view addSubview:like];
    
    //评论
    UIImageView *commentImgView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(like.frame), likeImgView.frame.origin.y, 20, 15)];
    commentImgView.image=[UIImage imageNamed:@"30X30-4.png"];
    [view addSubview:commentImgView];
    
    UILabel *comment=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(commentImgView.frame), time.frame.origin.y, 35, 35)];
    comment.text=_dataArray[section][@"commentNumber"];
    //comment.backgroundColor=[UIColor yellowColor];
    comment.textAlignment=NSTextAlignmentCenter;
    comment.textColor=[UIColor grayColor];
    comment.font=[UIFont systemFontOfSize:12];
    [view addSubview:comment];
    
    
    //排列评论人的头像
    CGFloat img_w=60/2.0;
    CGFloat img_h=60/2.0;
    NSInteger testLargeNumber=(self.frame.size.width-30.0/2.0)/(img_w+15/2.0);
    NSLog(@"==================%d",testLargeNumber);
    for (int i=0; i<[_dataArray[section][@"commentArr"] count]; i++) {
        if (i<testLargeNumber) {
            UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(30/2.0+(img_w+15/2.0)*i, CGRectGetMaxY(time.frame)+10/2.0, img_w, img_h)];
            imgView.image=[UIImage imageNamed:_dataArray[section][@"commentArr"][i][@"headImage"]];
            imgView.layer.cornerRadius=img_w/2.0;
            imgView.layer.masksToBounds=YES;
            [view addSubview:imgView];

        }
        
    }
    
    
    
    
    
    
    
    
    if ([_dataArray[section][@"commentArr"] count]>0) {
        //分割线
        UIView *line2=[[UIView alloc]initWithFrame:CGRectMake(30/2.0, CGRectGetMaxY(time.frame)+img_h+10/2.0+10/2.0, self.frame.size.width-30, 0.5)];
        line2.backgroundColor=[UIColor grayColor];
        [view addSubview:line2];
        view.frame=CGRectMake(0, 0, self.frame.size.width,CGRectGetMaxY(line2.frame));
    }else{
        view.frame=CGRectMake(0, 0, self.frame.size.width,CGRectGetMaxY(time.frame));
    }
    
    
    
    return view;
    
}

//计算行
-(NSInteger)getHang:(NSInteger)imageCount
{
    if (imageCount>1&&imageCount<=3) {
        return 1;
    }
    else{
        return 2;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSLog(@"2");
    UIView *view=[self makeView:section];
    return view.frame.size.height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

//根据文字计算宽度和高度
-(CGFloat)calculateWidthWithLable:(UILabel *)label
{
    CGSize size=[label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil]];
    return size.height;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
